﻿using Microsoft.EntityFrameworkCore;
using Wallpaper.Data.Infrastructure.Extensions;
using Wallpaper.Models.Entities;
using Wallpaper.Models.Interfaces;

namespace Wallpaper.Data.Context
{
    public class WallpaperDbContext : DbContext
    {
        public WallpaperDbContext(DbContextOptions<WallpaperDbContext> options)
            : base(options)
        {
        }

        // 用户相关
        public DbSet<User> Users { get; set; }
        public DbSet<Admin> Admins { get; set; }

        // 内容相关
        public DbSet<Wallpapers> Wallpapers { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Carousel> Carousels { get; set; }
        public DbSet<Tags> Tags { get; set; }
        public DbSet<WallpaperTag> WallpaperAndTags { get; set; }

        // 用户行为相关
        public DbSet<UserFavorite> UserFavorites { get; set; }
        public DbSet<DownloadRecord> DownloadRecords { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Menu> Menus { get; set; }
        public DbSet<RoleMenu>  RoleMenus { get; set; }
        public DbSet<Permission>  Permissions { get; set; }
        public DbSet<RolePermission>  RolePermissions { get; set; }

        public DbSet<Message> Messages { get; set; }
        
        public DbSet<UserMessage> UserMessages { get; set; }



        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            ////过滤掉继承ISoftDelete接口的IsDeleted为false
            //foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            //{
            //    if (typeof(ISoftDelete).IsAssignableFrom(entityType.ClrType))
            //    {
            //        var parameter = Expression.Parameter(entityType.ClrType, "e");
            //        var body = Expression.Equal(
            //            Expression.Property(parameter, "IsDeleted"),
            //            // 只返回 IsDeleted = false 的记录
            //            Expression.Constant(false));

            //        modelBuilder.Entity(entityType.ClrType)
            //            .HasQueryFilter(Expression.Lambda(body, parameter));
            //    }
            //}

            modelBuilder.ApplyGlobalFilters<ISoftDelete>("IsDeleted", false);

            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(typeof(WallpaperDbContext).Assembly);
        }



        // 可选：添加一些便捷的方法
        public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
        {
            // 软删除逻辑
            foreach (var entry in ChangeTracker.Entries())
            {
                if (entry.Entity is ISoftDelete softDeleteEntity &&
                    entry.State == EntityState.Deleted)
                {
                    entry.State = EntityState.Modified;
                    softDeleteEntity.IsDeleted = true;
                }
            }

            // 时间戳逻辑
            var entries = ChangeTracker.Entries()
                .Where(e => e.Entity is IHasTimestamps &&
                           (e.State == EntityState.Modified || e.State == EntityState.Added));

            foreach (var entityEntry in entries)
            {
                if (entityEntry.State == EntityState.Modified)
                {
                    ((IHasTimestamps)entityEntry.Entity).UpdatedAt = DateTime.Now;
                }

                if (entityEntry.State == EntityState.Added)
                {
                    ((IHasTimestamps)entityEntry.Entity).CreatedAt = DateTime.Now;
                }
            }

            return await base.SaveChangesAsync(cancellationToken);
        }
    }






}
